﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MainScene;

namespace SSShell
{
    public partial class ChangeNumber : Form
    {
        public frmMain _form;
        public ChangeNumber(frmMain Forma )
        {
            InitializeComponent();
            _form = Forma;

        }

        private void ChangeNumber_Load(object sender, EventArgs e)
        {
            int y, k;
            k =y= 0;
            foreach ( ClassNode item in ((ClassNode)_form.currentSelectedLink.Origin.Id).LstNodes)
            {
                if (item == (ClassNode)_form.currentSelectedLink.Destination.Id)
                {
                    y = k + 1;
                    k++;
                    oldvalue=y;
                    listBoxNumbers.Items.Add("Текущая ссылка");
                }
                else
                {
                    k++;
                    listBoxNumbers.Items.Add(k.ToString());
                }
            } 
         
           
        }
        public int oldvalue=0;
       
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        
        public int index = 0;
        Point p;

        private void listBoxNumbers_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.StringFormat))
            {
                string str = (string)e.Data.GetData(DataFormats.StringFormat);

                int inn = listBoxNumbers.IndexFromPoint(listBoxNumbers.PointToClient(new Point(e.X, e.Y)));
                if (inn >= 0 && inn < listBoxNumbers.Items.Count)
                {
                    string st = listBoxNumbers.Items[inn].ToString();
                    listBoxNumbers.Items.RemoveAt(listBoxNumbers.IndexFromPoint(p));
                    listBoxNumbers.Items.Insert(inn, str);
                    listBoxNumbers.SelectedIndex = inn;
                    
                }
            }
        }

        private void listBoxNumbers_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void listBoxNumbers_MouseDown(object sender, MouseEventArgs e)
        {
            index = listBoxNumbers.IndexFromPoint(e.X, e.Y);
             p.X = e.X;
             p.Y = e.Y;
             if (index >= 0 && index < listBoxNumbers.Items.Count)
             {
                 string s = listBoxNumbers.Items[index].ToString();
                 DragDropEffects dde1 = DoDragDrop(s, DragDropEffects.Copy);
             }
        }

        private void btnОК_Click(object sender, EventArgs e)
        {
            List<ClassNode> newListNodes = new List<ClassNode>();
            foreach (ClassNode item in ((ClassNode)_form.currentSelectedLink.Origin.Id).LstNodes)
            {
                newListNodes.Add(item);
            }
            ((ClassNode)_form.currentSelectedLink.Origin.Id).LstNodes.Clear();
            int y = 0;
            foreach (string item in listBoxNumbers.Items)
            {
                
                if (item == "Текущая ссылка")
                {
                    foreach (ClassNode nodee in newListNodes)
                    {
                        if (nodee == (ClassNode)_form.currentSelectedLink.Destination.Id)
                            ((ClassNode)_form.currentSelectedLink.Origin.Id).LstNodes.Add(nodee);
                    }
                }
                else
                {
                    int cc=1;
                    foreach (ClassNode nodee in newListNodes)
	                {
                        if (cc == int.Parse(item))
                        {
                            ((ClassNode)_form.currentSelectedLink.Origin.Id).LstNodes.Add(nodee);
                            break;
                        }
                        else
                            cc++;
	                }
                }
            }
        }
    }
}
